﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using VOL;
using System.Data;
using DAL;

namespace BLL
{
    public class ChiTietHoaDonBL
    {
        private DataAccess dataAccess;
        public ChiTietHoaDonBL()
        {
            dataAccess = new DataAccess();
        }

        //Thêm
        public bool insertChiTietHoaDon(ChiTietHoaDon obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };
            param[1] = new SqlParameter("@MaHang",SqlDbType.Int){Value = obj.MaHang};
            param[2] = new SqlParameter("@SoLuong", SqlDbType.Int) { Value = obj.SoLuong };
            param[3] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };

            string strSql = "INSERT INTO tbl_ChiTietHoaDon(MaHD, MaHang, SoLuong, DonGia) VALUES (@MaHD, @MaHang, @SoLuong, @DonGia)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        //Xóa
        public bool deleteChiTietHoaDon(ChiTietHoaDon obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };

            string strSql = "DELETE FROM tbl_ChiTietHoaDon WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Sửa
        public bool updateChiTietHoaDon(ChiTietHoaDon obj)
        {
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = obj.MaHD };
            param[1] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };
            param[2] = new SqlParameter("@SoLuong", SqlDbType.Int) { Value = obj.SoLuong };
            param[3] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };

            string strSql = "UPDATE tbl_ChiTietHoaDon SET " +
                            "MaHang = @MaHang, " +
                            "SoLuong = @SoLuong, " +
                            "DonGia = @DonGia " +
                            "WHERE MaHD = @MaHD";
            dataAccess.Execute(strSql);
            return true;
        }

        //Tìm
        public DataTable searchIDChiTietHoaDon(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };

            string strSql = "SELECT * FROM tbl_ChiTietHoaDon WHERE MaHD = @MaHD";
            return dataAccess.GetData(strSql,param);
        }

        public DataTable getDataReport(int MaHD)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHD", SqlDbType.Int) { Value = MaHD };

            string strSql = "SELECT tbl_DMHang.MaHang, tbl_DMHang.TenHang, tbl_ChiTietHoaDon.SoLuong, tbl_ChiTietHoaDon.DonGia, tbl_ChiTietHoaDon.SoLuong * tbl_ChiTietHoaDon.DonGia AS ThanhTien " +
                            " FROM tbl_ChiTietHoaDon INNER JOIN tbl_DMHang ON tbl_ChiTietHoaDon.MaHang = tbl_DMHang.MaHang WHERE MaHD = @MaHD";
            return dataAccess.GetData(strSql, param);
        }
        //Còn nhiều nữa
    }
}
